package cn.bluemobi.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.bluemobi.app.core.mybatis.PageUtils;
import cn.bluemobi.app.entity.output.Comment;
import cn.bluemobi.app.entity.output.Commodity;
import cn.bluemobi.app.entity.output.Specification;
import cn.bluemobi.app.mapper.CommodityMapper;
import cn.bluemobi.app.service.CommodityService;
import cn.bluemobi.app.service.TokenService;
import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.dto.Page;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class CommodityServiceImpl implements CommodityService {

	@Autowired
	private CommodityMapper commodityMapper;

	@Autowired
	private TokenService tokenService;

	@Override
	public Page<Commodity> findCommodityList(Integer pageNo, Integer pageSize,
			String keyword, String secondCategoryId, String isPointsExchange,
			String isThreeMultiplePoints, String isRecommend, String isNew,
			String priceSort, String buyCountSort) {
		Page<Commodity> page = PageUtils.startPage(pageNo, pageSize);
		Map<String, Object> params = new HashMap<>();
		params.put("keyword", keyword);
		params.put("secondCategoryId", secondCategoryId);
		params.put("isPointsExchange", isPointsExchange);
		params.put("isThreeMultiplePoints", isThreeMultiplePoints);
		params.put("isRecommend", isRecommend);
		params.put("isNew", isNew);
		params.put("priceSort", priceSort);
		params.put("buyCountSort", buyCountSort);
		List<Commodity> commodities = commodityMapper.findCommodityList(params);
		page.setList(commodities);
		return page;
	}

	@Override
	public Dto<Commodity> findCommodityDetail(String memberId,
			String commodityId) {
		Dto<Commodity> dto = new Dto<Commodity>();
		Commodity commodity = commodityMapper.findCommodityDetail(commodityId,
				memberId);
		if (commodity != null) {
			List<String> imageUrlList = commodityMapper
					.findCommodityImageUrlList(commodityId);
			commodity.setImageUrlList(imageUrlList);
			List<Specification> specificationList = commodityMapper
					.findCommoditySpecificationList(commodityId);
			commodity.setSpecificationList(specificationList);
		}
		dto.setData(commodity);
		return dto;
	}

	@Override
	public Dto<Specification> findSpecificationDetail(String commodityId,
			String specificationId) {
		Dto<Specification> dto = new Dto<Specification>();
		Specification specification = commodityMapper.findSpecificationDetail(
				commodityId, specificationId);
		dto.setData(specification);
		return dto;
	}

	@Override
	public Page<Comment> findComments(Integer pageNo, Integer pageSize,
			String commodityId) {
		Page<Comment> page = PageUtils.startPage(pageNo, pageSize);
		Map<String, Object> params = new HashMap<>();
		params.put("commodityId", commodityId);
		List<Comment> commentList = commodityMapper.findComments(params);
		if (!CollectionUtils.isEmpty(commentList)) {
			for (Comment comment : commentList) {
				List<String> imageUrlList = commodityMapper
						.findCommentImags(comment.getCommentId());
				comment.setImageUrlList(imageUrlList);
			}
		}
		page.setList(commentList);
		return page;
	}
}
